//
//  CarouselPicture.m
//  SoHuVideo
//
//  Created by dllo on 15/12/9.
//  Copyright © 2015年 Yusijia. All rights reserved.
//

#import "CarouselPicture.h"
#import "UIImageView+WebCache.h"
#import "UIImage+AFNetworking.h"
#import "AFNetworking.h"

#define KWIDTH self.frame.size.width
#define KHEIGHT self.frame.size.height
#define kuan  [UIScreen mainScreen].bounds.size.width / 375
#define gao [UIScreen mainScreen].bounds.size.height / 667
@interface CarouselPicture () <UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;

@property(nonatomic, retain)UIImageView *currentImageView;/**<当前图片*/

@property(nonatomic, retain)UIImageView *frontImageView;/**<上一张图片*/

@property(nonatomic, retain)UIImageView *behindImageView;/**<下一张图片*/

@property(nonatomic, retain)UIPageControl *pageControl;/**<页面控制器*/

@property(nonatomic, assign)NSInteger page;

@property(nonatomic, retain)NSTimer *timer;/**<定时器*/

@end
@implementation CarouselPicture
- (void)dealloc{
    [_scrollView release];
    [_currentImageView release];
    [_frontImageView release];
    [_behindImageView release];
    [_imageUrlArr release];
    [_urlArr release];
    [_titleArr release];
    [super dealloc];
}
#pragma mark - 初始化
- (id)initWithFrame:(CGRect)frame timeInterval:(CGFloat)timeInterval imageUrlArr:(NSArray <NSURL*> *)imageUrlArr urlArr:(NSArray *)urlArr titleArr:(NSArray *)titleArr {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        //属性接收传值
        _timeInterval = timeInterval;
        self.imageUrlArr = [NSMutableArray arrayWithArray:imageUrlArr];
        self.urlArr = [NSMutableArray arrayWithArray:urlArr];
        self.titleArr = [NSMutableArray arrayWithArray:titleArr];
       
        //设置定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
        
    if (imageUrlArr.count == 1) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:self.frame];
        [img sd_setImageWithURL:imageUrlArr.firstObject];
        return img;
    }else{
        //创建轮播图
        [self createCarousel];
    }
    }
    return self;
}


- (void)createCarousel{
    
    self.page = 0;
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    //添加到自身View
    [self addSubview:self.scrollView];
    
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    //整页滚动
    self.scrollView.pagingEnabled = YES;
    //设置代理人
    self.scrollView.delegate = self;
    //偏移量
    self.scrollView.contentSize = CGSizeMake(KWIDTH * 3, KHEIGHT);
    self.scrollView.contentOffset = CGPointMake(KWIDTH, self.scrollView.contentOffset.y);
    [_scrollView release];
    
    //添加tap 手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPicture)];
    self.currentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH, 0, KWIDTH, KHEIGHT)];
    //将手势添加到当前页上
    [self.currentImageView addGestureRecognizer:tap];
    [tap release];
    //开启交互
    self.currentImageView.userInteractionEnabled = YES;
    //获取图片(第一张图片)
    [self.currentImageView sd_setImageWithURL:self.imageUrlArr[0]];
    //将当前图片添加到scrollView上
    [self.scrollView addSubview:self.currentImageView];
    
    //添加图片上的文字
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(5 * kuan, 180 * gao, 355 * kuan, 60 * gao)];
    lable1.text = _titleArr[0];
    lable1.textColor = [UIColor whiteColor];
    lable1.numberOfLines = 3;
    [_currentImageView addSubview:lable1];
//    [lable1 release];
    
    [_currentImageView release];
    //上一张图片
    self.frontImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    //(接收到得图片数组中的最后一张)
    [self.frontImageView sd_setImageWithURL:self.imageUrlArr.lastObject];
    //添加图片上的文字
    UILabel *lable0 = [[UILabel alloc]initWithFrame:CGRectMake(5 * kuan, 180 * gao, 355 * kuan, 60 * gao)];
    lable0.text = _titleArr.lastObject;
    lable0.textColor = [UIColor whiteColor];
    lable0.numberOfLines = 3;
    [_frontImageView addSubview:lable0];
//    [lable0 release];

    
    //添加
    [self.scrollView addSubview:self.frontImageView];
    [_frontImageView release];
    //下一张
    self.behindImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH * 2, 0, KWIDTH, KHEIGHT)];
    //数组中第二张图片
    [self.behindImageView sd_setImageWithURL:self.imageUrlArr[1]];
    //添加图片上的文字
    UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(5 * kuan, 180 * gao, 355 * kuan, 60 * gao)];
    lable2.text = _titleArr[1];
    lable2.textColor = [UIColor whiteColor];
    lable2.numberOfLines = 3;
    [_behindImageView addSubview:lable2];
//    [lable2 release];

    [self.scrollView addSubview:self.behindImageView];
    [_behindImageView release];
    

    //页面控制器
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.imageUrlArr.count * 15, 20)];
    //中心
    self.pageControl.center = CGPointMake(self.frame.size.width / 2, self.pageControl.center.y);
    //点数
    self.pageControl.numberOfPages = self.imageUrlArr.count;
//    设置当前页为0
    self.pageControl.currentPage = 0;
    [self addSubview:self.pageControl];
    [_pageControl release];
}
#pragma mark- 开始拖拽定时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
    //先设置当前页为0 然后再循环
    _page = (_page + 1) % self.imageUrlArr.count;
    
    if (_page < self.imageUrlArr.count - 1 && _page > 0) {
        [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
        [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
        [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
        
    }
    else if(_page == self.imageUrlArr.count - 1){
        [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
        [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
        [self.behindImageView sd_setImageWithURL:self.imageUrlArr.firstObject];
    }
    else{
        [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
        [self.frontImageView sd_setImageWithURL:self.imageUrlArr.lastObject];
        [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
    }

        self.pageControl.currentPage = _page;

    
}
#pragma mark - 结束拖拽减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _page = self.pageControl.currentPage;
#pragma mark - 向左拖
    if (scrollView.contentOffset.x > KWIDTH) {
         _page = (_page + 1) % self.imageUrlArr.count;
        
        //判断1, 0,  -1三种情况
        if (_page < (NSInteger)(self.imageUrlArr.count - 1) && _page > 0) {
            [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
            self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
            [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
            [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
            self.pageControl.currentPage = _page;
        }
        else{
            if(_page == 0){
                [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
                self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
                [self.frontImageView sd_setImageWithURL:self.imageUrlArr.lastObject];
                [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
                self.pageControl.currentPage = 0;
            }
            else if (_page == self.imageUrlArr.count - 1) {
                [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
                self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
                [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
                [self.behindImageView sd_setImageWithURL:self.imageUrlArr.firstObject];
                self.pageControl.currentPage = self.imageUrlArr.count - 1;
                _page = -1;
            }

        }
        
    }
#pragma mark -向右拖
    if (scrollView.contentOffset.x < KWIDTH) {
         _page = (_page - 1 + self.imageUrlArr.count)  % self.imageUrlArr.count;
        
        
        //判断1 ,0 , -1三种情况
        if (_page > 0 && _page < (NSInteger)self.imageUrlArr.count - 1) {
            [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
            self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
            [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
            [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
            self.pageControl.currentPage = _page;
        }
        else{
            if (_page == 0) {
                [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
                self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
                [self.frontImageView sd_setImageWithURL:self.imageUrlArr.lastObject];
                [self.behindImageView sd_setImageWithURL:self.imageUrlArr[_page + 1]];
                self.pageControl.currentPage = 0;
            }
            else if (_page >= self.imageUrlArr.count - 1) {
                _page = self.imageUrlArr.count - 1;
                [self.currentImageView sd_setImageWithURL:self.imageUrlArr[_page]];
                self.scrollView.contentOffset = CGPointMake(KWIDTH, 0);
                [self.frontImageView sd_setImageWithURL:self.imageUrlArr[_page - 1]];
                [self.behindImageView sd_setImageWithURL:self.imageUrlArr.firstObject];
                self.pageControl.currentPage = self.imageUrlArr.count - 1;
            }
        }
    }
//    NSLog(@"%li", self.pageControl.currentPage);
    
#pragma mark - 定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
- (void)changeImage{
    
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + KWIDTH, 0) animated:YES];
    
}
- (void)clickPicture{
//    NSString *urlStr = self.imageUrlArr[self.page];
    [self.delegate pushWithUrl:_page];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
